<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\erp\home;
use app\common\controller\AuthCheck;

/**
 * 收货地址管理
 * @package app\cms\admin
 */
class Receiving extends AuthCheck{
    /**
     * 首页
     * @author shiqiang
     * @return json_out(code, message, data)
     */
    public function index(){
        //参数接收
        $params 		= $this->getParams(0);//0 get 1 post
        $user_id    	= intval(isset($params["user_id"]) ? $params["user_id"] : 0);//操作用户ID

        //数据验证
        $Validate = validate('Receiving', 'validate');
        if( !$Validate -> scene('receiving_index') -> check($params) ){
            json_out(-1001, $Validate -> getError(), []);//验证失败
        }

        //逻辑处理
        $list = controller('Receivinglogic', 'logic') -> index($user_id);

        //数据返回
        if($list['code'] == 200){
            json_out($list['code'], $list['message'], $list['data']);
        }else{
            json_out($list['code'], $list['message'], []);
        }
    }

    /**
     * 添加
     * @author shiqiang
     * @return json_out(code, message, (int)id)
     */
    public function add(){
        //参数接收
        $params 		= $this->getParams(1);//0 get 1 post
        $user_id    	= intval(isset($params["user_id"]) ? $params["user_id"] : 0);//操作用户ID
        $user_title    	= isset($params["user_title"]) ? $params["user_title"] : '';
        $user_city   	= isset($params["user_city"]) ? $params["user_city"] : '';
        $user_address   = isset($params["user_address"]) ? $params["user_address"] : '';
        $user_mobile   	= isset($params["user_mobile"]) ? $params["user_mobile"] : 0;
        $is_common   	= intval(isset($params["is_common"]) ? $params["is_common"] : 0);
        //数据验证
        $Validate = validate('Receiving', 'validate');
        if( !$Validate -> scene('receiving_add') -> check($params) ){
            json_out(-1001, $Validate -> getError());//验证失败
        }

        //逻辑处理
        $list = controller('Receivinglogic', 'logic') -> add($user_id, $user_title, $user_city, $user_address, $user_mobile, $is_common);
        //数据返回
        if($list['code'] == 200){
            json_out($list['code'], '新增成功');
        }else{
            json_out($list['code'], $list['message']);
        }
    }

    /**
     * 编辑
     * @author shiqiang
     * @return json_out(code, message, (int)id)
     */
    public function edit(){
        //参数接收
        $params 		= $this->getParams(1);//0 get 1 post
        $user_id    	= intval(isset($params["user_id"]) ? $params["user_id"] : 0);//操作用户ID
        $id 			= intval(isset($params["id"]) ? $params["id"] : 0);
        $user_title    	= isset($params["user_title"]) ? $params["user_title"] : '';
        $user_city    	= isset($params["user_city"]) ? $params["user_city"] : '';
        $user_address   = isset($params["user_address"]) ? $params["user_address"] : '';
        $user_mobile   	= isset($params["user_mobile"]) ? $params["user_mobile"] : 0;
        $is_common   	= intval(isset($params["is_common"]) ? $params["is_common"] : 0);

        //数据验证
        $Validate = validate('Receiving', 'validate');
        if( !$Validate -> scene('receiving_edit') -> check($params) ){
            json_out(-1001, $Validate -> getError());//验证失败
        }

        //逻辑处理
        $list = controller('Receivinglogic', 'logic') -> edit($user_id, $id, $user_city, $user_title, $user_address, $user_mobile, $is_common);

        //数据返回
        if($list['code'] == 200){
            json_out($list['code'], '编辑成功');
        }else{
            json_out($list['code'], $list['message']);
        }
    }

    /**
     * 删除
     * @param int $id
     * @author shiqiang
     * @return bool
     */
    public function delete(){
        //参数接收
        $params 	= $this->getParams(1);//0 get 1 post
        $user_id    = intval(isset($params["user_id"]) ? $params["user_id"] : 0);//操作用户ID
        $id 		= intval(isset($params["id"]) ? $params["id"] : 0);

        //数据验证
        $Validate = validate('Receiving', 'validate');
        if( !$Validate -> scene('receiving_delete') -> check($params) ){
            json_out(-1001, $Validate -> getError());//验证失败
        }

        //逻辑处理
        $list = controller('Receivinglogic', 'logic') -> delete($user_id, $id);

        //数据返回
        if($list['code'] == 200){
            json_out($list['code'], '删除成功');
        }else{
            json_out($list['code'], $list['message']);
        }
    }

}